//
//  MASettingViewController.m
//  MateAdv
//
//  Created by 茶 on 2023/9/18.
//

#import "MASettingViewController.h"
#import "MASettingTableViewCell.h"

@interface MASettingViewController ()

@property (nonatomic, strong) NSArray *titles;

@end

@implementation MASettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self initInterface];
}

- (void)initInterface {
    self.title = @"设置";

    [self.tableView registerClass:MASettingTableViewCell.class forCellReuseIdentifier:kSettingCellIdentifier];
    self.tableView.alwaysBounceVertical = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.rowHeight = MARatioWidth(50);
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.tableView.contentInset = UIEdgeInsetsMake(-UIDevice.statusHeight, 0, 0, 0);
    
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [loginBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:MAColor(@"#FF0071")];
    loginBtn.titleLabel.font = MAFontScaleMedium(16);
    loginBtn.layer.cornerRadius = MARatioWidth(24);
    loginBtn.clipsToBounds = YES;
    [loginBtn addTarget:self action:@selector(logoutEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).ma_offset(-36);
        make.height.ma_offset(48);
        make.width.ma_offset(327);
    }];
}

- (void)deleteEvent {
    MAWeakify(self);
    [LEEAlert alert].config
    .LeeTitle(@"温馨提示")         // 添加一个标题 (默认样式)
    .LeeContent(@"警告！警告！警告！\n注销会让用户数据永久消失\n确定注销吗？")        // 添加一个标题 (默认样式)
    .LeeCancelAction(@"取消", ^{    // 添加一个取消类型的Action (默认样式 alert中为粗体 actionsheet中为最下方独立)
    })
    .LeeAction(@"确定", ^{        //添加一个默认类型的Action (默认样式 字体颜色为蓝色)
        [ReqManager logoutWithSuccessed:^(id  _Nonnull object) {
            MAStrongify(self);
            [self clearUserInfo];
            [self.navigationController popToRootViewControllerAnimated:YES];
        } failed:^(NSError * _Nonnull error) {
            MAStrongify(self);
            [self.view makeToast:@"请求失败"];
        }];
    })
    .LeeShow();
}

- (void)logoutEvent {
    MAWeakify(self);
    [LEEAlert alert].config
    .LeeTitle(@"温馨提示")         // 添加一个标题 (默认样式)
    .LeeContent(@"确定退出当前账号吗？")        // 添加一个标题 (默认样式)
    .LeeCancelAction(@"取消", ^{    // 添加一个取消类型的Action (默认样式 alert中为粗体 actionsheet中为最下方独立)
    })
    .LeeAction(@"确定", ^{        //添加一个默认类型的Action (默认样式 字体颜色为蓝色)
        MAStrongify(self);
        [self clearUserInfo];
        [self.navigationController popToRootViewControllerAnimated:YES];
    })
    .LeeShow();
}

- (void)clearUserInfo {
    [kUserDefaults removeObjectForKey:kUserToken];
    [kUserDefaults removeObjectForKey:kUserInfo];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MASettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingCellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[MASettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSettingCellIdentifier];
    }
    cell.title = self.titles[indexPath.row];
    cell.index = indexPath.row;
    if (indexPath.row == 3) {
        cell.detailTitle = [NSString stringWithFormat:@"%ldM", MAUserInteger(kSavedData)];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
    } else if (indexPath.row == 1) {
    } else if (indexPath.row == 2) {
    } else if (indexPath.row == 3) {
        [kUserDefaults setInteger:0 forKey:kSavedData];
        [tableView reloadData];
    } else if (indexPath.row == 4) {
        [self deleteEvent];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = MAColor(@"#FF0071");
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return UIDevice.statusHeight + UIDevice.statusHeight + 30;
}

- (NSArray *)titles {
    if (_titles) return _titles;
    _titles = @[@"个性化推荐", @"版本", @"渠道", @"清除缓存", @"注销用户"];
    return _titles;
}

@end
